/*
 * @Author: your name
 * @Date: 2020-12-09 09:38:08
 * @LastEditTime: 2020-12-29 17:37:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \h5-zhuhaiyun20201209\js\zz\tool\tool.js
 */
(function() {
    // -----------------------------------------------------------
    /* 用户信息相关 ---S */
    window.zz_token = "XWI2TTIXTTCETTCQTTTMTTLML3LILNLJ0OLNLHL0TTCECQCLCXCNC0CTCICLCXCCCLCICLTMTTI0ICL0ITLNLOTTCETTLQLOLHLNLJTTIH";
    // --- 获取token（从地址栏获取）
    window.zzGetToken = function(info){
        try {
            window.zz_token = info;
        } catch (error) {
            console.log("---zzGetToken出错啦---",error);
        }
    };
    /* 用户信息相关 ---E */
    // -----------------------------------------------------------
    /* 获取项目路径 ---S */
    window.zzGetRealPath = function(){
        try {
            var localObj = window.location;
            var contextPath = localObj.pathname.split("/")[1];
            var basePath = localObj.protocol + "//" + localObj.host + "/" + contextPath + "/";
            return basePath;
        } catch (error) {
            return "---zzGetRealPath---";
        }
    }
    /* 获取项目路径 ---E */
    // -----------------------------------------------------------
    /* 获取location.search 或者 location.hash的参数值 ---S */
    window.zzGetHrefQueryString = function(name) {
        try {
            const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            const urlObj = window.location;
            var r = urlObj.href.indexOf('#') > -1 ? urlObj.hash.split("?")[1].match(reg) : urlObj.search.substr(1).match(reg);
            //console.log("---r---",r);
            if (r != null) return unescape(r[2]);
            return null;
        } catch (error) {
            console.log("---zzGetQueryString___error---",error);
            return null;
        }
    };
    /* 获取location.search 或者 location.hash的参数值 ---E */
    //-------------------------------------------------------------
    /* FormatTreeData ---S */
    function FormatTreeData(opt) {
        this.id = opt.id || "id";
        this.pId = opt.pId || "parent_id";
        this.emitId = opt.emitId || "0";
        this.treeJson = {};
    }
    FormatTreeData.prototype = {
        getTreeData:function(data) {
            //console.log(data);
            for (var i = 0, len = data.length; i < len; i++) {
                var t = data[i];
                if (!this.treeJson[t[this.pId]]) {
                    this.treeJson[t[this.pId]] = [];
                    this.treeJson[t[this.pId]].push(t);
                } else {
                    this.treeJson[t[this.pId]].push(t);
                }
            }
            //console.log(this.treeJson);
            //
            function format(pId) {
                var result = [];
                if (!this.treeJson[pId]) {
                    return result;
                }
                for (var i in this.treeJson[pId]) {
                    var t = this.treeJson[pId][i];
                    t.children = format.call(this, t[this.id]);
                    result.push(t);
                }
                return result;
            }
            return format.call(this, this.emitId);
        },
        transferTreeData:function(treeData) {
            let result = [];
            treeData.forEach(item => {
                result.push(item);
                pushDataToResult(item.children);
            });

            function pushDataToResult(arr) {
                while (arr.length) {
                    let item = arr.shift();
                    result.push(item);
                    if (item.children) {
                        pushDataToResult(item.children);
                    }
                }
            }
            result.forEach(item => {
                delete item.children;
            })
            return result;
        },
        reset:function(opt) {
            this.id = opt.id || "id";
            this.pId = opt.pId || "parent_id";
            this.emitId = opt.emitId || "0";
            this.treeJson = null;
            this.treeJson = {};
            return this;
        }
    };
    window.zzFormatTreeData = FormatTreeData;
    /* FormatTreeData ---E */
})();